package com.newdriver.edu.inf.manager.controller;
//客服
import com.newdriver.edu.inf.manager.domain.Student;
import com.newdriver.edu.inf.manager.service.Service;

import javax.swing.text.DefaultStyledDocument;
import java.util.Scanner;

public class StudentController {
    private Service service = new Service();
    private Scanner sc=new Scanner(System.in);
    public void start() {

        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choise = sc.next();
            switch (choise){
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStuById();

                    break;
                case "3":
                   // System.out.println("修改学生");
                    undateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用");
                    break lo;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    public void undateStudent() {
        String updateId;
        if (service.findAllStudent()==null) {
            System.out.println("空");
            return;
        }

        while (true) {
            System.out.println("输入要修改的学生id");
            updateId = sc.next();
            boolean exists = service.isExists(updateId);
            if(!exists){
                System.out.println("要修改的id不存在");
                break;
            }else{
                break;
            }
        }
        System.out.println("输入学生姓名");
        String name =sc.next();
        System.out.println("输入学生年龄");
        String age =sc.next();
        System.out.println("输入学生生日");
        String bir =sc.next();

        Student newStudent = new Student();
        newStudent.setId(updateId);
        newStudent.setName(name);
        newStudent.setAge(age);
        newStudent.setBir(bir);

        service.updateStudent(updateId,newStudent);

        System.out.println("修改成功");




    }

    public void deleteStuById() {
        String delId;
        //拿到id
        if (service.findAllStudent()==null) {
            System.out.println("空");
            return;
        }
        while (true) {
            //判断是否存在
            System.out.println("输入要删除的学生id");
             delId = sc.next();
            boolean exists = service.isExists(delId);
            if (!exists) {
                System.out.println("输入的id不存在");
            }else{
                break;
            }
        }
        //调用service
            service.delStuById(delId);
        //输出结果
        System.out.println("删除成功");
    }

    public void findAllStudent() {
        //调用业务员中的获取方法
        Student[] stus=service.findAllStudent();
        //判断是否为null
        if (stus==null) {
            System.out.println("暂无信息 先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t\t生日");
        //遍历打印
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu!=null) {
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBir());
            }
        }

    }

    public void addStudent() {
        //Service service = new Service();
        //接受
        Scanner sc=new Scanner(System.in);
        String id;

        while (true) {
            System.out.println("输入id");
            id = sc.next();
            boolean flag=service.isExists(id);
            if (flag) {
                System.out.println("学号已被占用");
            }else{
                break;
            }
        }


        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        String age = sc.next();
        System.out.println("输入生日");
        String bir = sc.next();
        //封装
        Student stu=new Student();
        //传递业务员
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBir(bir);
        boolean result = service.addStudent(stu);

        //打印结果
        if (result) {
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
}
